<template>
  <div class="frame-container" >
    <Header @toLoginEmit="$refs.toLogin.scrollIntoView()" @app-click="onAppClick" ></Header>
    <div class="frame-body">
      <div class="frame-body-wrapper">
        <div class="section adv-section">
          <h1 class="adv-text">开源企业</h1>
          <p style="width: 650px; line-height: 50px">
            持续创新的引擎，助力企业IT基础设施现代化、数字化转型、应用程序开发和应用程序现代化
          </p>
        </div>
        <div class="section">
          <div class="section-title">
            <div class="box">
              <span>产品应用</span>
            </div>
          </div>
          <el-row :gutter="20" v-if="getLocalToken()" v-loading="isLoading" element-loading-text="加载应用..." style="min-height: 200px;">
            <div v-if="myApps.length > 0" >
              <el-col class="item" :span="6" v-for="(item, index) in myApps" :key="index" @click.native="onAppClick(item.appKey)">
                <div>
                  <div class="item-top">
                    <div class="img">
                      <span class="img-title">{{item.appName}}</span>
                      <el-image :src="item.appPic"></el-image>
                    </div>
                  </div>
                  <div class="item-body">
                    <div class="item-title">{{item.appName}}</div>
                    <div class="item-center">{{item.desc}}</div>
                    <div class="item-footer">
                      <div class="text">{{item.user}}</div>
                      <el-dropdown>
                      <span class="el-dropdown-link">
                        <i class="el-icon-more el-icon--right"></i>
                      </span>
                        <el-dropdown-menu slot="dropdown">
                          <el-dropdown-item icon="el-icon-video-play">暂未开放</el-dropdown-item>
                        </el-dropdown-menu>
                      </el-dropdown>
                    </div>
                  </div>
                </div>
              </el-col>
            </div>
            <div v-else-if="myApps.length === 0 && !isLoading">
              <el-empty :image-size="100" description="暂无应用"></el-empty>
            </div>
          </el-row>
          <el-row v-else>
            <div class="is-login" ref="toLogin">
              <span>请先登录</span>
              <Login></Login>
            </div>
          </el-row>
        </div>
        <div class="section">
          <el-row :gutter="20">
            <el-col :span="12">
              <div class="adv-item">
                <div class="title">
                  <span>公告</span>
                  <span>更多<i class="el-icon-arrow-right"></i></span>
                </div>
                <p v-for="(item, index) in notices" :key="index">
                  <span>{{item.content}}</span>
                  <i class="el-icon-arrow-right"></i>
                </p>
              </div>
            </el-col>
            <el-col :span="12">
              <div class="adv-item">
                <div class="title">
                  <span>下载</span>
                  <span>更多<i class="el-icon-arrow-right"></i></span>
                </div>
                <p v-for="(item, index) in downloads" :key="index">
                  <span>{{item.content}}</span>
                  <i class="el-icon-arrow-right"></i>
                </p>
              </div>
            </el-col>
          </el-row>
        </div>
      </div>
    </div>

    <footer class="frame-footer">
      Copyright © 2023 上海新致软件股份有限公司
    </footer>
  </div>
</template>

<script>
import Header from './components/Header'
import { getLocalToken ,getLocalCurrentTenant ,getLocalCurrentCompany } from '@/utils/auth'
import { remoteCompanyAll , remoteTenantAll } from '@/api/platform.js'
import Login from '../Login'
export default {
  name: "Platform",
  components: { Header, Login },
  data() {
    return {
	  isLoading: true,
      getLocalToken,
      notices: [
          { content: '[案例]FM财务管理应用发布，涵盖标准产品代码框架、组织架构接口、标准应用接口' },
          { content: '[案例]HR人力资源管理应用发布，涵盖标准产品代码框架、组织架构接口、标准应用接口' },
          { content: '[案例]CRM客户管理应用发布，涵盖标准产品代码框架、组织架构接口、标准应用接口' },
          { content: '[案例]PM项目管理应用发布，涵盖标准产品代码框架、组织架构接口、标准应用接口' },
          { content: '[案例]关于开源企业平台搭建线上发布通知' },
      ],
      downloads: [
        { content: '应用部署发布说明文档' },
        { content: '及时通信服务器集成，文件下载的说明手册' },
        { content: '微服务架构技术文档' },
        { content: '产品应用开发指南（应用创建、功能开发、共享组件/工具类/Api、中间件、发布等）' },
      ],
      myApps: [],
      appLists: [],
      appSearch: '',
      activeName: 'first',
      localApps: [
		{
		  appName: '开发者平台',
		  appKey: 'devTool',
		  entry: process.env.VUE_APP_DEVTOOL_URL.slice(5)
		}
      ]
    }
  },
  created() {
	if (getLocalToken()) {
	  this.localApps.push({
		appName: '组织架构',
		appKey: 'organization',
		entry: process.env.VUE_APP_ORGANIZATION_URL.slice(5)
	  })
	}
    this.initPage([])
    this.getCurrentUserApps();
  },
  mounted() {

  },
  methods: {
    initPage(apps) {
      // 模拟应用参考
      // const appData = {
      //   myApps: [
      //     { id: 1, appName: 'FM财务管理', appKey: 'vueAppA', entry: '//localhost:8081', icon: 'el-icon-files', desc: '标准产品代码框架、组织架构接口、标准应用接口', user: '来自上海新致软件股份有限公司', appPic: require('../../assets/app1@3x.png'), isState: 0 },
      //     { id: 2, appName: 'HR人力资源管理', appKey: 'vueAppB', entry: '//localhost:8082', icon: 'el-icon-files', desc: '标准产品代码框架、组织架构接口、标准应用接口', user: '来自上海新致软件股份有限公司', appPic: require('../../assets/app2@3x.png'), isState: 0 },
      //     { id: 3, appName: 'CRM客户管理', appKey: 'lease', entry: '//localhost:3000', icon: 'el-icon-office-building', desc: '标准产品代码框架、组织架构接口、标准应用接口', user: '来自上海新致软件股份有限公司', appPic: require('../../assets/app3@3x.png'), isState: 0 },
      //     { id: 4, appName: 'PM项目管理', appKey: 'devTool', entry: '//localhost:3333/devTool/', icon: 'el-icon-office-building', desc: '标准产品代码框架、组织架构接口、标准应用接口', user: '来自上海新致软件股份有限公司', appPic: require('../../assets/app4@3x.png'), isState: 0 },
      //     { id: 4, appName: 'PM项目管理', appKey: 'devTool', entry: '//localhost:3333/devTool/', icon: 'el-icon-office-building', desc: '标准产品代码框架、组织架构接口、标准应用接口', user: '来自上海新致软件股份有限公司', appPic: require('../../assets/app4@3x.png'), isState: 0 },
      //   ]
      // }
      // if (appData) {
      //   this.myApps = appData.myApps
      // }
      // sessionStorage.setItem('appData', JSON.stringify(appData))

	  let myApps = [...apps, ...this.localApps]
	  const appData = {myApps}
	  sessionStorage.setItem('appData', JSON.stringify(appData))
    },
    toLogin() {
      this.$router.push('/login')
    },
    randomRgb() {
      let R = Math.floor(Math.random() * 255);
      let G = Math.floor(Math.random() * 255);
      let B = Math.floor(Math.random() * 255);
      return "rgb("+ R + "," + G + "," + B + ")";
    },
    onAppClick(appKey) {
      let routeData = this.$router.resolve({
        path: `/${appKey}`
      })
      let win = window.open(routeData.href, appKey)
    },
    getCurrentUserApps(){
      let search_List = {
        params: []
      }
      if(getLocalCurrentTenant() && getLocalCurrentTenant().id != '0' && getLocalCurrentCompany() && getLocalCurrentCompany().id == '0'){
        //租户管理员
        console.log('租户管理员------')
        search_List.params.push.apply(search_List.params, [
          {
            columnName: 'tenant_id',
            queryType: '=',
            value: getLocalCurrentTenant() ? getLocalCurrentTenant().id : '0'
          }
        ])
        remoteTenantAll(search_List).then(responseData => {
          //处理数据结果
          if(responseData.code == 100){
            this.isLoading = false
            const data = responseData.data;
            let apps = [];
            for (const datum of data) {
             const app = {
                id: datum.id,
                appName: datum.applicationVersion?datum.applicationVersion.app.fullName : '',
                appKey: datum.applicationVersion?datum.applicationVersion.app.appKey : '',
                entry: datum.applicationVersion.url,
                icon: datum.applicationVersion?datum.applicationVersion.app.iconId : '',
                desc: datum.applicationVersion?datum.remarks : '',
                user: datum.applicationVersion?datum.applicationVersion.app.createBy : '',
                appPic: require('../../assets/app1@3x.png'),
                isState: 0
             }
              apps.push(app)
            }
            this.myApps = apps
            this.initPage(apps)
          } else {
			this.isLoading = false
          }
        })
      }else{
		console.log('公司管理员和其他公司员工------')
        //公司管理员和其他公司员工
        search_List.params.push.apply(search_List.params, [
          {
            columnName: 'company.tenant_id',
            queryType: '=',
            value: getLocalCurrentTenant() ? getLocalCurrentTenant().id : '0'
          },
          {
            columnName: 'company_id',
            queryType: '=',
            value: getLocalCurrentCompany() ?  getLocalCurrentCompany().id : '0'
          }
        ])
        remoteCompanyAll(search_List).then(responseData => {
		  //处理数据结果
          if(responseData.code == 100){
            this.isLoading = false
			const data = responseData.data;
			let apps = [];
            for (const datum of data) {
              const app = {
                id: datum.id,
                appName: datum.applicationVersion?datum.applicationVersion.app.fullName : '',
                appKey: datum.applicationVersion?datum.applicationVersion.app.appKey : '',
                entry: datum.applicationVersion.url,
                icon: datum.applicationVersion?datum.applicationVersion.app.iconId : '',
                desc: datum.applicationVersion?datum.applicationVersion.remarks : '',
                user: datum.applicationVersion?datum.applicationVersion.app.createBy : '',
                appPic: require('../../assets/app1@3x.png'),
                isState: 0
              }
              apps.push(app)
            }

			this.myApps = apps
			this.initPage(apps)
          } else {
			this.isLoading = false
          }
        }).catch(() => {

        })
      }
    }
  }
}
</script>

<style scoped lang="scss">
  @import '../../styles/element-variables.scss';
.frame-container {
  background: url("../../assets/web-bg.png") no-repeat center top 60px;
  background-size: 100%;
  .frame-top {
    height: 75px;
    background-size: 100% 100%;
    background-color: #fff;
    margin-bottom: 18px;
    // background-image: url("../../assets/top_bg.png");
    background-repeat: no-repeat;
    .frame-top-content {
      max-width: 1180px;
      margin: 0 auto;
      height: 100%;
      display: flex;
      justify-content: space-between;
      align-items: center;
      span {
        font-size: 18px;
        color: #1b1e21;
        font-weight: 600;
      }
    }
  }
  .frame-body {
    overflow: hidden;
    .frame-body-wrapper {
      max-width: 1280px;
      margin: 0 auto;
    }
    .section {
      /*padding: 20px 20px 12px;*/
      /*border-radius: 6px;*/
      margin-bottom: 18px;
      &.adv-section {
        min-height: 360px;
        background: url("../../assets/web-figure@3x.png") no-repeat right 150px top 50px / 30%;
        padding: 0 80px;
        .adv-text {
          display: inline-block;
          font-size: 50px;
          font-weight: 700;
          padding: 50px 0 0 0;
          color: #333;
          position: relative;
          margin: 0;
          z-index: 2;
          &:after {
            position: absolute;
            content: '';
            width: 70%;
            height: 20px;
            background-color: #d9e5f9;
            border-radius: 20px;
            bottom: 0;
            left: 50%;
            margin-left: -35%;
            z-index: -1;
          }
        }
        p {
          padding: 30px 0 0 0;
          font-size: 24px;
          margin: 0;
        }
      }
      .item {
        padding: 8px;
        margin: 2px 0;
        position: relative;
        /*&:after {
          content: '';
          width: 80%;
          height: 30px;
          transition: all .3s;
          position: absolute;
          bottom: -2px;
          left: 50%;
          margin-left: -40%;
          border-radius: 10px;
          background-color: #fff;
          box-shadow: 0 4px 10px 0 rgba(142,142,147,.15);
          z-index: 1;
        }*/
        &:hover {
          &:after {
            bottom: -5px;
          }
          > div {
            box-shadow: 0 4px 20px 0 rgba(142,142,147,.2);
          }
        }
        > div {
          cursor: pointer;
          border-radius: 10px;
          overflow: hidden;
          position: relative;
          z-index: 2;
          transition: all .3s;
          background-color: #ffffff;
          box-shadow: 0 4px 10px 0 rgba(142,142,147,.15);
        }
        .item-top, .item-title, .item-center, .item-footer {
          display: flex;
          align-items: center;
        }
        .item-body {
          padding: 20px;
        }
        .item-center {
          height: 36px;
          line-height: 18px;
        }
        .item-center, .item-footer {
          margin-top: 10px;
        }
        .item-title {
          font-size: 16px;
        }
        .item-top {
          .img {
            width: 100%;
            overflow: hidden;
            height: 110px;
            display: flex;
            justify-content: left;
            align-items: center;
            .img-title {
              position: absolute;
              font-size: 20px;
              left: 10px;
              color: #fff;
              z-index: 2;
            }
            i {
              width: 40px;
              height: 40px;
              font-size: 20px;
              color: #fff;
              border-radius: 6px;
              display: flex;
              justify-content: center;
              align-items: center;
            }
          }
          span {
            display: block;
            font-weight: 500;
            height: 28px;
            width: 154px;
            font-size: 16px;
            color: #171a1d;
            line-height: 28px;
            overflow: hidden;
            white-space: nowrap;
            text-overflow: ellipsis;
            margin-left: 10px;
          }
        }
        .item-center {
          overflow: hidden;
          text-overflow: ellipsis;
          display: -webkit-box;
          -webkit-line-clamp: 2;
          -webkit-box-orient: vertical;
          font-size: 12px;
          color: #a2a3a5;
        }
        .item-footer {
          justify-content: space-between;
          .text {
            font-size: 12px;
            color: #a2a3a5;
          }
          .el-dropdown {
            padding: 6px 8px;
            &:hover {
              background-color: #f5f5f5;
              border-radius: 3px;
            }
          }
          i {
            margin: 0;
          }
        }
      }
    }
    .adv-item {
      background-image: linear-gradient(to bottom, rgba(235, 241, 249), rgba(249, 251, 255));
      padding: 15px;
      border-radius: 10px;
      border: 4px solid #ffffff;
      box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
      box-sizing: border-box;
      min-height: 231px;
      .title {
        font-size: 14px;
        color: #999;
        display: flex;
        padding: 10px 0;
        justify-content: space-between;
        align-items: center;
        span {
          cursor: pointer;
        }
        span:first-child {
          font-size: 18px;
          font-weight: 700;
          cursor: default;
          color: $--color-primary;
        }
      }
      p {
        cursor: pointer;
        margin: 0;
        line-height: 30px;
        position: relative;
        display: flex;
        align-items: center;
        justify-content: space-between;
        span {
          overflow: hidden;
          white-space: nowrap;
          text-overflow: ellipsis;
        }
        &:hover {
          color: $--color-primary;
        }
      }
    }
    .section-title {
      margin-bottom: 18px;
      .box {
        text-align: center;
        padding: 50px 0;
        span {
          font-size: 30px;
          color: #333333;
          font-weight: 700;
        }
      }
      .el-tabs {
        ::v-deep .el-tabs__item {
          font-weight: normal;
        }
        ::v-deep .el-tabs__header {
          margin: 0;
        }
        ::v-deep .el-tabs__nav-wrap::after {
          height: 1px;
        }
        ::v-deep .el-tabs__active-bar {
          /*margin-left: -10px;
          width: 20px!important;
          height: 3px;*/
        }
      }
      span {
        font-size: 16px;
        font-weight: 600;
      }
      .search {

      }
    }
  }
  .is-login {
    //background: url('../../assets/appBg.png') no-repeat center top / cover;
    min-height: 300px;
  }
  .frame-footer {
    padding: 30px 0;
    margin-top: 50px;
    border-top: 1px solid #F5F5F5;
    text-align: center;
  }
}
</style>
